package cn.yj.common.service;

import cn.yj.alipay.emums.AlipayOrderStatus;
import cn.yj.common.emums.PayType;
import cn.yj.common.factory.SignFactory;
import cn.yj.common.interfaces.IPaySuccessNotify;
import cn.yj.commons.utils.MapUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.tools.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理异步结果通知
 *
 * @author 永健
 * @since 2022-02-19 13:45
 */
@Component
public class PaySuccessNotifyHandler {

    private static final Logger log = LoggerFactory.getLogger(PaySuccessNotifyHandler.class);

    @Autowired(required = false)
    private List<IPaySuccessNotify> notifies;

    /**
     * 下单的时候主动去查询订单支付状态-且成功的通知
     * 或者是前端调起支付，付款后的主动请求回调
     *
     * @param payType    微信还是支付宝
     * @param outTradeNo 商户订单号
     */
    public void syncNotify(PayType payType, String outTradeNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("outTradeNo", outTradeNo);
        this.loopNotifyListener(payType, false, params);
    }


    /**
     * 支付平台异步通知的结果需要验签的
     *
     * @param payType 微信还是支付宝
     * @param params  通知的原始参数
     */
    public void syncNotify(PayType payType, Map<String, ?> params) {
        log.info("支付结果通知，payType={},params={}", payType.name(), params.toString());
        SignFactory signFactory = SignFactory.build(payType).selectExecutor();
        switch (payType) {
            case ALIPAY:
                // 先验签
                String errorMsg = "验签失败";
                String trade_status = MapUtils.getStringVal((Map<String, Object>) params, "trade_status");
                if (AlipayOrderStatus.TRADE_SUCCESS.name().equals(trade_status)) {
                    if (signFactory.deSign((Map<String, String>) params)) {
                        loopNotifyListener(PayType.ALIPAY, true, params);
                        return;
                    }
                }
                throw new ServiceException(errorMsg);
            case WECHAT:
                signFactory.deSign((Map<String, String>) params);
                loopNotifyListener(PayType.WECHAT, true, params);
                break;
            default:
                loopNotifyListener(null, true, params);
                break;
        }
    }

    private void loopNotifyListener(PayType payType, boolean tag, Map<String, ?> params) {
        if (StringUtils.isNull(notifies)) {
            return;
        }
        for (IPaySuccessNotify notify : notifies) {
            switch (payType) {
                case ALIPAY:
                    notify.alipay(params, tag);
                    break;
                case WECHAT:
                    notify.wechat(params, tag);
                    break;
                default:
                    notify.other(params, tag);
                    break;
            }
        }
    }
}
